<template>
  <div>
    <!-- 注册界面（已完成） -->
    <!-- 导航栏部分 -->
    <div class="nav">
      <div class="left">
        <router-link to="/login">
          <img src="./imgs/left(white).svg" height="20px" width="20px" />
        </router-link>
      </div>
      <div class="center">用户注册</div>
    </div>

    <!-- 背景图 -->
    <div class="pic1 pwdPic" v-show="showPic">
      <p>夏日粥铺</p>
    </div>
    <div class="pic2 pwdPic" v-show="!showPic">
      <p>夏日粥铺</p>
    </div>

    <div class="login">
      <div class="phone">
        <div class="left-text">手机号</div>
        <input
          type="text"
          placeholder="请输入您的手机号"
          value="phone"
          v-model="phone"
        />
      </div>
      <div class="code">
        <div class="left-text">短信验证码</div>
        <input
          type="code"
          placeholder="请输入验证码"
          value="code"
          v-model="code"
        />
        <button 
        :class="{right_phone:rightPhone}" 
        :disabled='!rightPhone'
        @click="getCode"
        >
        {{computeTime?`已发送(${computeTime}s)`:'发送验证码'}}
        </button>
      </div>
      <div class="pwd">
          <div class="left-text">密&nbsp;&nbsp;&nbsp;码</div>
          <input
            type="text"
            placeholder="请输入密码"
            value="upwd"
            v-model="upwd"
            ref='inputPwd'
            @focus="showPic = false"
            @blur="showPic = true"
            v-show="showPwd"
          />
          <input
            type="password"
            placeholder="请输入密码"
            value="upwd"
            v-model="upwd"
            ref='inputPwd'
            @focus="showPic = false"
            @blur="showPic = true"
            v-show="!showPwd"
          />
          <span class="switch" @click="showPwd=!showPwd">
            <img src="./imgs/show.svg" v-show="showPwd">
            <img src="./imgs/hidden.svg" v-show="!showPwd">
          </span>
        </div>
    </div>

    <!-- 按钮部分 -->
    <button class="btn" @click="checkForm">快速注册</button>
  </div>
</template>

<style lang='scss' scoped>
/* 导航栏部分 */
.nav {
  height: 40px;
  background-color: #26a2fe;
  color: white;
  line-height: 40px;
  .center {
    position: absolute;
    left: 50%;
    transform: translate(-50%, 0);
  }
  .left {
    width: 30px;
    margin: 0 10px;
    background: url(/imgs/left.png) no-repeat;
  }
}
// 背景图部分
.pwdPic {
  width: 100%;
  height: 90px;
  p {
    text-align: center;
    line-height: 70px;
    font-size: 20px;
    font-weight: 700;
    color: #48cfe5;
  }
}
.pic1 {
  background: url(./imgs/b1.png);
  background-size: 100% 100%;
}
.pic2 {
  background: url(./imgs/b2.png);
  background-size: 100% 100%;
}

/* 登录框部分 */
.login {
  position: relative;
  height: 150px;
  width: 100%;
  margin-top: 10px;
}
.phone,
.code,
.pwd {
  width: 100%;
  position: absolute;
  padding-left: 10px;
  height: 35px;
  border-bottom: 1px solid #edeff2;
  .left-text {
    position: absolute;
    text-align: center;
    line-height: 30px;
  }
}

/* 给所有的输入框和按钮设置公共样式 */
input {
  position: absolute;
  left: 100px;
  border: none;
  height: 30px;
}
::-webkit-input-placeholder {
  color: #cbc9d5;
}
button {
  border: none;
}
.code {
  top: 55px;
  button {
    float: right;
    padding: 0 15px;
    font-size: 14px;
    margin: 3.5px 8px;
    background-color: #fff;
    border-left: 1px solid #cbc9d5;
    color: #cbc9d5;
  }
  .right_phone {
    color: black;
  }
}
.pwd {
  top: 110px;
  .right_phone {
    color: black;
  }
  .switch {
    float: right;
    margin-right: 15px;
    img {
      height: 30px;
    }
  }
}
.code > input {
  width: 120px;
}
/* 发送验证码按钮 */
.code > button {
  float: right;
  margin-right: 8px;
  font-size: 14px;
  margin: 3.5px 8px;
}
/* 登录按钮 */
.btn {
  width: 95%;
  background-color: #26a2fe;
  margin: 8px;
  border-radius: 5px;
  color: white;
}
</style>

<script>
export default {
  data() {
    return {
      phone: "",
      code: "",
      upwd: "",
      computeTime: 0, //发送验证码倒计时
      showPic: true, //密码框获得焦点时，背景图的变化
      showPwd: false
    };
  },
  computed: {
    rightPhone() {
      return /^1[3-9]\d{9}$/.test(this.phone);
    }
  },
  methods: {
    // 获取验证码
    getCode() {
      //启动倒计时
      if (!this.computeTime) {
        //当 computeTime 为0时，说明此时还没开始计时
        this.computeTime = 60;
        const countDown = setInterval(() => {
          this.computeTime--;
          if (this.computeTime === 0) {
            clearInterval(countDown);
          }
        }, 1000);
      }
    },
    // 验证手机号是否正确
    checkPhone() {
      let reg = /^1[3-9]\d{9}$/;
      if (reg.test(this.phone)) {
        return true;
      } else {
        this.$toast({
          message: "手机号格式不正确！",
          position: "center",
          duration: 3000
        });
        return false;
      }
    },
    // 短信验证码
    checkCode() {
      let reg = /^\w{4}$/;
      if (reg.test(this.code)) {
        return true;
      } else {
        this.$toast({
          message: "请输入正确的验证码！",
          position: "center",
          duration: 3000
        });
        return false;
      }
    },
    // 验证密码格式
    checkUpwd() {
      let reg = /^\w{6,20}$/;
      if (reg.test(this.upwd)) {
        return true;
      } else {
        this.$toast({
          message: "请输入6-20位由数字、字母、_组成的密码！",
          position: "center",
          duration: 3000
        });
        return false;
      }
    },
    // 点击提交按钮后，发送 axios 请求
    checkForm() {
      if (this.checkPhone() && this.checkCode() & this.checkUpwd()) {
        // alert("发送axios请求");
        this.axios
          .post(
            "/api/user/userRegistered",
            `phone=${this.phone}&userPwd=${this.upwd}`
          )
          .then(result => {
            // console.log(result);
            var res = result.data;
            if (res.code == 200) {
              this.$toast({
                message: `注册成功！`,
                position: "center",
                duration: 3000
              });
              this.$router.push("/login");
            } else if (res.code == 402) {
              this.$toast({
                message: `该手机号已注册，请直接登录！`,
                position: "center",
                duration: 3000
              });
              this.$router.push("/login");
            } else {
              this.$toast({
                message: `注册失败！`,
                position: "center",
                duration: 3000
              });
            }
          });
      }
    }
  }
};
</script>